<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <title>客户表</title>
    <script src="../../../static/kaige/js/szload.js"></script>
</head>

<body>
<div class="kaige-body">
    <table class="layui-table" id="pageTable" lay-filter="pageTable" lay-size="sm"></table>
    <input type="hidden" name="keywordsCache" value="{}"/>
</div>

<script>
    load_init_js(function () {
        layui.config({
            base: szoapcpath + '/static/kaige/layuiextend/'
        }).use(['element', 'table', 'form', 'layer', 'kaige', 'laytpl'], function () {
            var element = layui.element,
                layer = layui.layer,
                table = layui.table,
                form = layui.form,
                kaige = layui.kaige,
                laytpl = layui.laytpl,
                $ = layui.$;
            window.kaige_page = {
                logout_url: '/index/logout',
                welcome: './pages/welcome.html',
                menus: [],
                leftmenu: [],
            };

            window.pagedict = parent.window.kaige_page.dict || {};

            form.render();

            kaige.table({
                elem: '#pageTable',
                url: "/crm.open_customer/index",
                id: 'pageTable',
                toolbar: '#dtoolbar',
                defaultToolbar: ['filter'],
                cols: [[
                    {field: 'id', title: '', align: 'center'},
                    {field: 'name', title: '客户名称', align: 'center'},
                    {field: 'subname', title: '助记名称', align: 'center'},
                    {field: 'level', title: '客户星级', align: 'center', templet: "#level_name"},
                    {field: 'belong', title: '客户所属', align: 'center', templet: "#belong_name"},
                    {field: 'source', title: '客户来源', align: 'center', templet: "#source_name"},
                    {field: 'address', title: '省市区ID', align: 'center'},
                    {field: 'address_detail', title: '详细地址', align: 'center'},
                    {field: 'remarks', title: '备注信息', align: 'center'},
                    {field: 'create_time', title: '创建时间', align: 'center'},
                    {title: '操作', toolbar: '#dtrbar', align: 'center', width: 150, fixed: 'right'}
                ]]
            });

            //列表-监听表头工具栏事件dtoolbar
            table.on('toolbar(pageTable)', function (obj) {
                switch (obj.event) {
                    case 'btn-add':
                        btnadd();
                        break;
                    case 'btn-auto':
                        btnauto();
                        break;
                }
            });

            //列表-监听单元格行工具事件dtrbar
            table.on('tool(pageTable)', function (obj) {
                var that = this;
                var data = obj.data;
                switch (obj.event) {
                    // case 'edit':
                    //     baredit(data);
                    //     break;
                    case 'get':
                        getEvent(data);
                        break;
                }
            });

            function getEvent(d) {
                layer.confirm('您确定领取[' + d.name + ']吗？', {
                    title: '<i class="layui-icon layui-icon-delete"></i> 删除确认',
                    btn: ['确认', '取消']
                }, function () {
                    $.post("/crm.open_customer/get", {id: d.id}, function (res) {
                        res.status && table.reload('pageTable');
                        layer.msg(res.msg);
                    });
                });
            };


            window.addEventListener('message', function (event) {
                // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
                var loc = event.data;
                if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
                    console.log('location', loc);
                }
            }, false);


            window.showPage = function () {
                $.get("/crm.open_customer/show_address", {}, function (res) {
                    var content = $('#address_form_html').html();
                    laytpl(content).render(res.data, function (reshtml) {
                        layer.open({
                            id: 'address_form_box',
                            type: 1,
                            anim: 4,
                            area: ['400px', '100%'],
                            title: "地图",
                            offset: 'r',
                            content: reshtml,
                            btn: ['确定', '取消'],
                            yes: function (index, layero) {
                                layer.close(index);
                            },
                        })
                    })
                })
            }

            function autounit(page, limit) {
                szoa.post('/crm.open_customer/auto', {page: page, limit: limit}, function (res) {
                    layer.msg(res.msg);
                    if (res.status && res.data.page) {
                        autounit(res.data.page, limit)
                    } else {
                        res.status && table.reload('pageTable');
                    }
                })
            }

            function btnauto() {
                autounit(1, 1);
            }

            //添加
            function btnadd() {
                window.addEventListener('message', function (event) {
                    // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
                    var loc = event.data;
                    if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
                        $("#address").val(loc.poiaddress);
                        $("#address_detail").val(loc.poiname);
                        $("#address_detail").val(loc.poiname);
                        $("#lat").val(loc.latlng.lat);
                        $("#lng").val(loc.latlng.lng);
                    }
                }, false);

                kaige.form({
                    url: "/crm.open_customer/add",
                    data: {},
                    id: 'kaige',
                    area: ['750px', '450px'],
                    title: '<i class="layui-icon layui-icon-add-circle"></i> 添加 - 客户表',
                    tablelayid: 'pageTable',
                    overflow: false,
                    encode: 1,
                    callbefore: function (layero, index) {
                        return false;
                    }
                });
            }

            //编辑
            function baredit(d) {
                kaige.form({
                    url: "/crm.open_customer/edit",
                    data: {id: d.id},
                    id: 'kaige',
                    area: ['750px', '450px'],
                    title: '<i class="layui-icon layui-icon-edit"></i> 编辑 - 客户表',
                    tablelayid: 'pageTable',
                    overflow: false,
                    encode: 1,
                    callbefore: function (layero, index) {

                    },
                });
            }

            //删除
            function bardel(d) {
                layer.confirm('您确定删除吗？', {
                    title: '<i class="layui-icon layui-icon-delete"></i> 删除确认',
                    btn: ['确认', '取消']
                }, function () {
                    $.post("/crm.customer/del", {id: d.id}, function (res) {
                        res.status && table.reload('pageTable');
                        layer.msg(res.msg);
                    });
                });
            };

        });
    });
</script>
<script type="text/html" id="dtrbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-xs" lay-event="get" title="领取">领取</button>
    </div>
</script>
<script type="text/html" id="dtoolbar">
    <div class="layui-btn-container layui-form filter-form" lay-filter="searchform">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn layui-btn-sm" lay-event="btn-addbtn-add" style="margin-top:2px;"><i
                    class="layui-icon layui-icon-add-circle"></i> 添加
            </button>
            <button type="button" class="layui-btn layui-btn-sm" lay-event="btn-auto" style="margin-top:2px;"><i
                    class="layui-icon layui-icon-add-circle"></i> 自动入海
            </button>
        </div>
    </div>
</script>


<script type="text/html" id="level_name">
    {{# for(var ipd in pagedict.clue_level){ }}
    {{# if(d.level ==ipd){ }}
    <span>{{ pagedict.clue_level[ipd] }}</span>
    {{# } }}
    {{# } }}
</script>


<script type="text/html" id="belong_name">
    {{# for(var ipd in pagedict.custome_belong){ }}
    {{# if(d.belong ==ipd){ }}
    <span>{{ pagedict.custome_belong[ipd] }}</span>
    {{# } }}
    {{# } }}
</script>

<script type="text/html" id="source_name">
    {{# for(var ipd in pagedict.clue_source){ }}
    {{# if(d.source ==ipd){ }}
    <span>{{ pagedict.clue_source[ipd] }}</span>
    {{# } }}
    {{# } }}
</script>

<script type="text/html" id="kaige_form_html">
    <form class="layui-form layui-form-pane kaige-body-form" style="padding: 5px;" lay-filter="kaige_form_lay_filter">
        <input type="hidden" name="lng" id="lng" id="lng">
        <input type="hidden" name="lat" id="lat" id="lat">
        {{# if(d.id){ }}
        <input type="hidden" name="id" value="{{ d.id }}"/>
        {{# } }}
        <div class="layui-form-item">
            <label class="layui-form-label">客户名称</label>
            <div class="layui-input-inline">
                <input name="name" value="{{ d.name || '' }}" class="layui-input" type="text">
            </div>
            <label class="layui-form-label">助记名称</label>
            <div class="layui-input-inline">
                <input name="subname" value="{{ d.subname || '' }}" class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户星级</label>
            <div class="layui-input-inline">
                <select name="level" lay-verify="required">
                    {{# lay.each(pagedict.clue_level,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->跟进类型未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.level== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>
            <label class="layui-form-label">客户所属</label>
            <div class="layui-input-inline">
                <select name="belong" lay-verify="required">
                    {{# lay.each(pagedict.custome_belong,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->客户所属未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.belong== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户来源</label>
            <div class="layui-input-inline">
                <select name="source" lay-verify="required">
                    {{# lay.each(pagedict.clue_source,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->客户来源未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.source== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>
            <label class="layui-form-label">跟进状态</label>
            <div class="layui-input-inline">
                <select name="follow" lay-verify="required">
                    {{# lay.each(pagedict.followStatus,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->客户所属未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.follow== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <input name="remarks" value="{{ d.remarks || '' }}" class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地理位置</label>
            <div class="layui-input-block">
                <input id="address" name="address" value="{{ d.address || '' }}" class="layui-input" type="text"
                       onfocus="showPage();">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">详细地址</label>
            <div class="layui-input-block">
                <input id="address_detail" name="address_detail" value="{{ d.address_detail || '' }}"
                       class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <button class="layui-hide" lay-submit="" id="kaige_form_btn_save" lay-filter="kaige_form_btn_save">保存数据
            </button>
        </div>
    </form>
</script>

<script type="text/html" id="address_form_html">
    <input type="hidden" name="allclose" value="1"/>
    <div style="width: 100%;height: 500px">
        <iframe id="mapPage"
                src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key={{ d.set }}&referer=crm"
                width="100%" height="100%" frameborder="0">
        </iframe>
    </div>
</script>
</body>

</html>